# coding:utf-8
import MySQLdb
import os
import os.path
# 引入七牛
from qiniu import Auth, put_file, etag, urlsafe_base64_encode
import qiniu.config
import time
conn = MySQLdb.connect(
    host="数据库地址",
    port=3306,
    user="用户名",
    passwd="数据库密码",
    db="数据库名",
    charset="utf8"
)
conrsor = conn.cursor()
video_id = 0    # 存下来的方便后面如果出现错误能顺利进行下一条
while True:
    sql = "select * from 视频表 where normal_compress=1 and height_compress=0 and normal_upload=0 and height_upload=1 and is_edit=0 and id>%s order by id limit 0,1;"    # 查出没有上传高清和标清的视频
    conrsor.execute(sql %video_id)
    print conrsor.rowcount  # 打印出条数
    if conrsor.rowcount > 0:
        rs = conrsor.fetchone()  # 打印出查询结果的第一条
        print rs
 
        path = os.getcwd()   # 当前执行程序路径
        suffix = os.path.splitext(rs[1])[1]  # 文件后缀
        file_name = os.path.splitext(rs[1])[0]       # 文件名字(不包括后缀)
        # file_name_normal = file_name + "_final_normal"
        file_normal_name = file_name + "_final_normal" + suffix     # 标清文件名
        file_high_name = file_name + "_final" + suffix   # 高清文件名
        file_normal = os.path.join(path + "\\upload\\final\\", file_normal_name)    #上传的并转换后的视频放在upload的final文件夹里面
        file_high = os.path.join(path + "\\upload\\final\\", file_high_name)
        print file_high, file_normal
        access_key = 'AK'
        secret_key = 'SK'
        # 构建鉴权对象
        q = Auth(access_key, secret_key)
        # 要上传的空间
        bucket_name = '空间名'
        # 生成上传 Token，可以指定过期时间等
        try:
            key = file_normal_name  # 传标清文件
            token = q.upload_token(bucket_name, key, 3600)
            ret, info = put_file(token, key, file_normal)
            assert ret['key'] == key
            assert ret['hash'] == etag(file_normal)
            print rs[0]
            if (info.status_code == 200):    # 上传成功
                try:
                    key = file_high_name  # 传高清文件
                    token = q.upload_token(bucket_name, key, 3600)
                    ret, info = put_file(token, key, file_high)
                    assert ret['key'] == key
                    assert ret['hash'] == etag(file_high)
 
                    if(info.status_code == 200):
                        url = "URL/" + ret['key']
                        conrsor.execute("UPDATE 视频表 SET normal_upload=1 where id=%d" % rs[0])
                        conrsor.execute("update 视频表 set height_upload=1 ,third_url = '%s' where id=%s" % (url, rs[0]))
 
                        conn.commit()
                        video_id = rs[0]
                except Exception as e:
                    print e
                    conn.rollback()
        except UnicodeEncodeError as e:
            print e
            print "文件名出错或者没有该文件"
            video_id = rs[0]    # 如果文件名有问题，就跳过当前的视频
 
    else:
        time.sleep(60)
        
conrsor.close()
conn.close()            # 关闭数据库连接
	
# coding:utf-8
import MySQLdb
import os
import os.path
# 引入七牛
from qiniu import Auth, put_file, etag, urlsafe_base64_encode
import qiniu.config
import time
conn = MySQLdb.connect(
    host="数据库地址",
    port=3306,
    user="用户名",
    passwd="数据库密码",
    db="数据库名",
    charset="utf8"
)
conrsor = conn.cursor()
video_id = 0    # 存下来的方便后面如果出现错误能顺利进行下一条
while True:
    sql = "select * from 视频表 where normal_compress=1 and height_compress=0 and normal_upload=0 and height_upload=1 and is_edit=0 and id>%s order by id limit 0,1;"    # 查出没有上传高清和标清的视频
    conrsor.execute(sql %video_id)
    print conrsor.rowcount  # 打印出条数
    if conrsor.rowcount > 0:
        rs = conrsor.fetchone()  # 打印出查询结果的第一条
        print rs
 
        path = os.getcwd()   # 当前执行程序路径
        suffix = os.path.splitext(rs[1])[1]  # 文件后缀
        file_name = os.path.splitext(rs[1])[0]       # 文件名字(不包括后缀)
        # file_name_normal = file_name + "_final_normal"
        file_normal_name = file_name + "_final_normal" + suffix     # 标清文件名
        file_high_name = file_name + "_final" + suffix   # 高清文件名
        file_normal = os.path.join(path + "\\upload\\final\\", file_normal_name)    #上传的并转换后的视频放在upload的final文件夹里面
        file_high = os.path.join(path + "\\upload\\final\\", file_high_name)
        print file_high, file_normal
        access_key = 'AK'
        secret_key = 'SK'
        # 构建鉴权对象
        q = Auth(access_key, secret_key)
        # 要上传的空间
        bucket_name = '空间名'
        # 生成上传 Token，可以指定过期时间等
        try:
            key = file_normal_name  # 传标清文件
            token = q.upload_token(bucket_name, key, 3600)
            ret, info = put_file(token, key, file_normal)
            assert ret['key'] == key
            assert ret['hash'] == etag(file_normal)
            print rs[0]
            if (info.status_code == 200):    # 上传成功
                try:
                    key = file_high_name  # 传高清文件
                    token = q.upload_token(bucket_name, key, 3600)
                    ret, info = put_file(token, key, file_high)
                    assert ret['key'] == key
                    assert ret['hash'] == etag(file_high)
 
                    if(info.status_code == 200):
                        url = "URL/" + ret['key']
                        conrsor.execute("UPDATE 视频表 SET normal_upload=1 where id=%d" % rs[0])
                        conrsor.execute("update 视频表 set height_upload=1 ,third_url = '%s' where id=%s" % (url, rs[0]))
 
                        conn.commit()
                        video_id = rs[0]
                except Exception as e:
                    print e
                    conn.rollback()
        except UnicodeEncodeError as e:
            print e
            print "文件名出错或者没有该文件"
            video_id = rs[0]    # 如果文件名有问题，就跳过当前的视频
 
    else:
        time.sleep(60)
        
conrsor.close()
conn.close()            # 关闭数据库连接